package com.hyphenate.easeim.common.db

import android.text.TextUtils
import com.hyphenate.chat.EMClient
import com.hyphenate.easeim.common.db.dao.AppKeyDao
import com.hyphenate.easeim.common.db.entity.AppKeyEntity
import com.hyphenate.easeim.common.manager.OptionsHelper

/**
 * @Time:2021/5/6 15:32
 * @Author:wujinsheng
 * @Description:
 */
object AppKeyRepository : AppKeyDao by DemoDbHelper.getInstance().appKeyDao {

    val appKeys: List<AppKeyEntity>
        get() {
            val defAppkey = OptionsHelper.getInstance().defAppkey
            val appKey = EMClient.getInstance().options.appKey
            if (!TextUtils.equals(defAppkey, appKey)) {
                val appKeys = queryKey(appKey)
                if (appKeys == null || appKeys.isEmpty()) {
                    insert(AppKeyEntity(appKey))
                }
            }
            return loadAllAppKeys()
        }

    /**
     * 保存appKey
     *
     * @param appKey
     */
    fun saveAppKey(appKey: String?) {
        val entity = AppKeyEntity(appKey!!)
        insert(entity)
    }
}